﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;
using Ciripescu_WebRole.Properties;

namespace Ciripescu_WebRole
{
    public partial class profile : BasePageAnonAndLoggedIn
    {
        string userName = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            
            if (Request.QueryString["UserName"] != null)
                userName = Request.QueryString["UserName"];
            if (string.IsNullOrEmpty(userName))
            {
                errors.InnerHtml = Resources.ErrNoProfileSelected;
                return;
            }
            UserManager um = new UserManager();
            User user = um.GetItem(userName);
            if (user == null)
            {
                errors.InnerHtml = Resources.ErrUserNotExists;
                return;
            }

            this.Title = user.FullName + " (" + user.GetUserName() + ") pe Ciripescu.ro";

            if (Session["UserName"] == null)
            {
                not_logged_in_notice.Visible = true;
                btnSendPrivateMessage.Visible = false;
                btnFollow.Visible = false;
            }
            else
            {
                not_logged_in_notice.Visible = false;
                if (((string)Session["UserName"]).CompareTo(userName) == 0) //userrul isi vede profilul
                {
                    btnSendPrivateMessage.Visible = false;
                    btnFollow.Visible = false;
                }
                else
                {
                    UserManager usrManag = new UserManager();
                    if(usrManag.Follows((string)Session["UserName"], userName))
                       btnFollow.Visible = false;
                    else
                       btnFollow.Visible = true;

                    btnSendPrivateMessage.Visible = true;
                }
            }

            lblCompleteName.Text = user.PartitionKey + "/" + user.FullName;
            if (user.Location != String.Empty)
            {
                lblLocation.Text = user.Location;
                lblLocation.Visible = true;
            }
            else
                lblLocation.Visible = false;

            if (string.IsNullOrEmpty(user.TaggedURL))
                aTagged.Visible = false;
            else
                if (!user.TaggedURL.StartsWith("http://"))
                    aTagged.HRef = "http://" + user.TaggedURL;
                else
                    aTagged.HRef = user.TaggedURL;

            if (string.IsNullOrEmpty(user.Hi5URL))
                aHi5.Visible = false;
            else
                if (!user.Hi5URL.StartsWith("http://"))
                    aHi5.HRef = "http://" + user.Hi5URL;
                else
                    aHi5.HRef = user.Hi5URL;


            if (string.IsNullOrEmpty(user.InfoURL))
                aMyPage.Visible = false;
            else
                if (!user.InfoURL.StartsWith("http://"))
                    aMyPage.HRef = "http://" + user.InfoURL;

                else
                    aMyPage.HRef = user.InfoURL;

            if (user.OneLineBio != String.Empty)
                lblOneLineBio.Text = user.OneLineBio;
            else
                lblOneLineBio.Visible = false;

            imgProfile.ImageUrl = user.PictureUri;
            UrmarireManager uManag  = new UrmarireManager(); 
            lblNrUrmariti.Text     = uManag.GetNrUrmariti(userName).ToString();
            lblNrUrmaritori.Text   = uManag.GetNrUrmaritori(userName).ToString();

        }

        
        public string GetUserName()
        {
            if (Request.QueryString["UserName"] != null)
                return Request.QueryString["UserName"];
            else return Resources.ErrError;
        }

        protected void btnFollow_Click(object sender, ImageClickEventArgs e)
        {
            UrmarireManager urmManag = new UrmarireManager();
            string toFollow = String.Empty;
            if (Request.QueryString["UserName"] != null)
                toFollow = Request.QueryString["UserName"];
            if (string.IsNullOrEmpty(toFollow))
            {
                errors.InnerHtml = Resources.ErrBadUserToFollow;
                return;
            }
            else
            {
                UserManager um = new UserManager();
                User user = um.GetItem(toFollow);
                if (user == null)
                {
                    errors.InnerHtml = Resources.ErrUserNotExists;
                    return;
                }
                else
                {
                    Urmarire urm = new Urmarire((string)Session["UserName"],user.PartitionKey);
                    urmManag.Insert(urm);
                    btnFollow.Visible = false;
                    lblNrUrmariti.Text = urmManag.GetNrUrmariti(userName).ToString();
                    lblNrUrmaritori.Text = urmManag.GetNrUrmaritori(userName).ToString();
                }
            }
        }

        protected void btnSendPrivateMessage_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/sendmessage/" + GetUserName()+"/");
        }
    }
}
